#include "str.h"

#include <assert.h>
#include <string.h>

static void test_str_from_cstr(void)
{
    const char * hello = "hello";
    str_t *  cstr = str_create_from_cstr(hello);
    assert(str_length(cstr) == strlen(hello));
    assert(str_capacity(cstr) == str_length(cstr));

    assert(strcmp(str_const_data(cstr), hello) == 0);

    assert(str_const_data(cstr)[str_length(cstr)] == 0);
    str_println(cstr);
    str_destroy(cstr);
}

static void test_str_as_zero_array(void)
{
    size_t zero_array_len = 1024;
    str_t * zero_arr_str = str_create_as_zero_array(zero_array_len);
    assert(str_length(zero_arr_str) == 0);
    assert(str_capacity(zero_arr_str) == zero_array_len - 1 );
    
    assert(str_const_data(zero_arr_str)[0] == 0);
    assert(str_const_data(zero_arr_str)[zero_array_len - 1] == 0);
    str_println(zero_arr_str);
    str_destroy(zero_arr_str);
}

static void test_str_from_array(void)
{
    char array[] = {0x31, 0x32, 0x33, 0x34};
    str_t * arr_str = str_create_from_array(array, sizeof(array) + 10, sizeof(array));
    assert(str_capacity(arr_str) == sizeof(array) + 9);
    assert(str_length(arr_str) == sizeof(array));
    assert(str_const_data(arr_str)[sizeof(array)] == 0);

    assert(str_const_data(arr_str)[0] == 0x31);
    assert(str_const_data(arr_str)[1] == 0x32);
    assert(str_const_data(arr_str)[2] == 0x33);
    assert(str_const_data(arr_str)[3] == 0x34);
    str_println(arr_str);

    str_data(arr_str)[1] = 'a';
    assert(str_data(arr_str)[1] == 'a');
    str_println(arr_str);
    str_destroy(arr_str);
}


extern int sge_packet_encrypt(void *cli_session, str_t * src, str_t * dst, void * cli_ctx, int * err_code){
    assert(str_capacity(dst) >= str_length(src));
    strncpy(str_data(dst), str_const_data(src), str_length(src));
    str_set_length(dst, str_length(src));
    return 0;
}

static void test_encrypt(void){
    /* SMELL : data clumps
    char src[1024] = "abc";
    char dst[1024] = {0};
    size_t dst_len = sizeof(dst);
    char plaintext[1024] = {0};
    size_t plaintext_len = sizeof(plaintext);
    //SMELL : long parameter list
    ASSERT_EQ(0, sge_packet_encrypt(cli_session_handle, (unsigned char *)src, strlen(src), (unsigned char *)dst, &dst_len, cli_ctx, &err_code));
    ASSERT_EQ(0, err_code) << "client: sge_packet_encrypt expect err_code: 0, actually: " << err_code;
    */

    str_t * src = str_create_from_cstr("abc");
    assert(str_length(src) == strlen("abc"));

    str_t * dst = str_create_as_zero_array(1024);
    assert(str_capacity(dst) == 1024 -1 );

    str_t * plaintext = str_create_as_zero_array(1024);

    void * cli_session = NULL;
    void * cli_ctx = NULL;
    int err_code = 0;
    assert(sge_packet_encrypt(cli_session, src, dst, cli_ctx, &err_code) == 0);
    assert(str_length(src) == str_length(dst));
    str_println(dst);

    str_destroy(plaintext);
    str_destroy(dst);
    str_destroy(src);
}

#include "ood.h"

int main(){
    ood_test();

    //test_str_from_cstr();
    //test_str_as_zero_array();
    //test_str_from_array();

    //test_encrypt();
    return 0;
}

